<?php

namespace App\Common\OA;


use App\Common\Log;
use App\Model\t_s_account;
use App\X\EHelper;
use EasySwoole\Component\Openssl;
use XWX\Common\H;

class OA extends _Base
{
    //账号验证码值
    static public function funcVCodeKey__redis($ip)
    {
        return "vcode:{$ip}";
    }

    //账号登陆错误次数
    static public function funcLoginErrCountKey__redis($acc_uid)
    {
        return "login_err_count:{$acc_uid}";
    }


    static public function funcGetLoginKey__redis($id)
    {
        return "OA:{$id}";
    }

    static public function funcGetSignKey()
    {
        return "QWE123qwe!@#" . H::today()->format('Ymd');
    }

    static public function funcKey($x)
    {
        $key = "{$x}!!123abc";

        return $key;
    }


    static public function funcAdminCheckSign($key)
    {
        $base_data = base64_decode($key);
        $json = json_decode($base_data);

        if ($json == null)
        {
            return false;
        }

        $sign = self::funcAdminGetSign($json->x, $json->acc_id, $json->nonce_str);
        if ($sign == $json->sign)
        {
            return true;
        }

        return false;
    }

    /**
     * 获取签名
     *
     * @param $x
     * @param $acc_id
     * @param $nonce_str
     * @return string
     */
    static public function funcAdminGetSign($x, $acc_id, $nonce_str)
    {
        $enc_code__list =
            [
                $x,
                $acc_id,
                self::funcGetSignKey(),
                $nonce_str,
            ];
//        var_dump($enc_code__list);
        sort($enc_code__list);
//        var_dump($enc_code__list);
        $enc_code__pre = join('-', $enc_code__list);
        Log::funcLogDebug(__METHOD__, $enc_code__pre);

        $sign = H::funcStrMD5($enc_code__pre);
        Log::funcLogDebug(__METHOD__, "sign={$sign}");


        return $sign;
    }


    /**
     * 获取加密后的密码
     *
     * @param $x
     * @param $pwd
     * @return string
     */
    static public function funcGetEncPwd($x, $pwd)
    {
        $pwd_enc = H::funcStrMD5("{$x}|{$pwd}");

        return $pwd_enc;
    }


    /**
     * acc
     *
     * @param $user_name
     * @return t_s_account|null
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function funcGetAccByUserName($user_name)
    {
        $query = static::db()
            ->where('acc_lock', 0);
        if (H::funcStrContains($user_name, '@'))
        {
            Log::funcLogDebug(__METHOD__, "{$user_name}-子账号登陆");

            $acc_data = explode('@', $user_name);
            $acc_uid__super = $acc_data[0];
            $arr_acc__super = static::db()
                ->where('acc_uid', $acc_uid__super)
                ->getOne(t_s_account::table());
            if ($arr_acc__super === null)
            {
                return null;
            }

            $acc_uid = $acc_data[1];;

            $query
                ->where('acc_x_id', $arr_acc__super['acc_x_id'])
                ->where('acc_uid', $acc_uid);
        }
        else
        {
            Log::funcLogDebug(__METHOD__, "{$user_name}-超级管理员登陆");

            $query->where('acc_uid', $user_name);
        }


        $arr_acc = $query->getOne(t_s_account::table());
        if ($arr_acc === null)
        {
            return null;
        }

        $entity_acc = new t_s_account($arr_acc);

        return $entity_acc;
    }


    /**
     * 获取文件访问链接
     *
     * @param $val
     * @return string
     */
    static function funcGetFileUrl($val)
    {
        $root = EHelper::funcGetConf('SYS.static_file_root');

        return "{$root}_upload/{$val}";
    }


    /**
     * 输入图片数组（前端用）
     *
     * @param $str
     * @return array
     */
    static function funcGetUploadFileList($str)
    {
        $str_items = explode(',', $str);

        $list = [];
        foreach ($str_items as $file_val)
        {
            if (H::funcStrIsNullOrEmpty($file_val))
            {
                continue;
            }

            $list[] = [
                'id' => $file_val,
                'imgId' => $file_val,
                'display' => OA::funcGetFileUrl($file_val),
            ];
        }


        return $list;
    }
}